package com.anjoyo.xgh.musicshare.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.anjoyo.xgh.musicshare.R;
import com.anjoyo.xgh.musicshare.adapter.ShareAdapter;
import com.anjoyo.xgh.musicshare.api.ShareApi;
import com.anjoyo.xgh.musicshare.application.SongApplication;
import com.anjoyo.xgh.musicshare.db.MusicDBhelper;
import com.anjoyo.xgh.musicshare.entry.ShareBean;
import com.anjoyo.xgh.musicshare.entry.Song;
import com.anjoyo.xgh.musicshare.entry.User;
import com.anjoyo.xgh.musicshare.util.Constant;
import com.anjoyo.xgh.musicshare.util.MusicSpf;
import com.anjoyo.xgh.musicshare.util.PullToRefreshListView;
import com.anjoyo.xgh.musicshare.util.PullToRefreshListView.OnRefreshListener;
import com.anjoyo.xgh.musicshare.util.StringUtil;

public class ShareListAct extends Activity {

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == Constant.MSG_SHARELIST_OK) {
				Toast("分享列表下载成功");
				shareBeans = (ArrayList<ShareBean>) msg.obj;
				adapter = new ShareAdapter(con, shareBeans, listView, handler);
				listView.setAdapter(adapter);
				// adapter.notify();
			}
			if (msg.what == Constant.MSG_SHARE_BACK) {
				switch (msg.arg1) {
				case 1:
					ShareBean shareBean = (ShareBean) msg.obj;
					String url = shareBean.getFile();
					if (!StringUtil.isnull(url)) {
						PlaySounds(url);
						Toast("缓冲中……");
					} else {
						Toast("没有录音");
					}
					break;
				case 2:
					ShareBean shareBean2 = (ShareBean) msg.obj;
					Song song = shareBean2.getMusic();
					if (song != null && !StringUtil.isnull(song.getNet_url())) {
						PlaySounds(song.getNet_url());
						Toast("缓冲中……");
					} else {
						Toast("没有音乐");
					}
					break;
				case 3:
					ShareBean shareBean3 = (ShareBean) msg.obj;
					shareApi.likeShare(shareBean3.getId(), con);
					break;
				default:
					break;
				}
			}
			super.handleMessage(msg);
		}
	};
	Context con;
	PullToRefreshListView listView;
	ArrayList<ShareBean> shareBeans;
	ShareAdapter adapter;
	ShareApi shareApi;
	MediaPlayer mediaPlayer;
	static String temp;
	MusicDBhelper dBhelper;
	User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sharelist_layout);
		con = this;
		dBhelper = new MusicDBhelper(con);
		listView = (PullToRefreshListView) findViewById(R.id.lv_sharelist);
		shareApi = new ShareApi(handler);
		user = MusicSpf.getloginUser(con);
		if (dBhelper.getShareList() != null) {
			Message message = new Message();
			message.what = Constant.MSG_SHARELIST_OK;
			message.obj = dBhelper.getShareList();
			handler.sendMessage(message);
			System.out.println("数据库有数据");
		}else {
			shareApi.getShareList(user.getId(), "", dBhelper);
		}
		if (SongApplication.getInstance().player.isPlaying()) {
			SongApplication.getInstance().player.pause();
			SongApplication.getInstance().ispaused=true;
		}
		
		mediaPlayer = new MediaPlayer();
		listView.setOnRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				shareApi.getShareList(user.getId(), "", dBhelper);
				listView.onRefreshComplete();
			}
		});
		findViewById(R.id.iv_share).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(con, ShareAct.class));
			}
		});
		findViewById(R.id.iv_back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	public void Toast(String msg) {
		Toast.makeText(con, msg, Toast.LENGTH_SHORT).show();
	}

	protected void PlaySounds(String url) {
		if (url.equals(temp) && !StringUtil.isnull(url)) {
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.pause();
				temp = url;
				return;
			} else {
				mediaPlayer.start();
				return;
			}
		}
		temp = url;
		mediaPlayer.reset();
		try {
			mediaPlayer.setDataSource(Constant.SERVER_IP + url);
			mediaPlayer.prepareAsync();
			mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					mp.start();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mediaPlayer.isPlaying()) {
			mediaPlayer.stop();
		}
		if (SongApplication.getInstance().ispaused) {
			SongApplication.getInstance().getPlayer().start();
			SongApplication.getInstance().ispaused=false;
		}
	}
}
